Imports MyClasses
Imports DAL
Imports System.Data

Public Class BScheme
    Public Shared Function getSchemeItem(ByVal ItemID As Integer, ByVal InvoiceDate As Date) As DataTable
        Return DScheme.getSchemeItem(ItemID, InvoiceDate)
    End Function

    Public Shared Function Save(ByRef scheme As CScheme, ByVal saveupdate As String) As Integer
        Return DScheme.Save(scheme, saveupdate)
    End Function
    '          04/10/2007
    Public Shared Function GetSchemeType(ByVal SchemeID As Integer) As Char
        Return DScheme.GetSchemeType(SchemeID)
    End Function
    '          05/10/2007
    Public Shared Function GetSchemeDiscount(ByVal SchemeID As Integer) As Double
        Return DScheme.GetSchemeDiscount(SchemeID)
    End Function
    ' 18/10/2007
    Public Shared Function GetScheme(ByVal SchemeID As Integer) As CScheme
        Dim dtl As DataTable
        Dim csch As New CScheme
        Try
            dtl = DScheme.GetScheme(SchemeID)
            If dtl.Rows.Count > 0 Then
                With dtl.Rows(0)
                    csch.SchemeID = .Item("SchemeID")
                    csch.ItemID = .Item("ItemID")
                    csch.SchemeType = .Item("SchemeType")
                    csch.BuyerCategoryID = .Item("BuyerCategoryCD")
                    csch.SellerCategoryID = .Item("SellerCategoryCD")
                    csch.ValidFrom = .Item("ValidFrom")
                    csch.ValidTo = .Item("Validto")
                    csch.SchemeMinQty1 = .Item("SchemeMinQty1")
                    csch.SchemeFreeQty1 = .Item("SchemeFreeQty1")
                    csch.SchemeMinQty2 = .Item("SchemeMinQty2")
                    csch.SchemeFreeQty2 = .Item("SchemeFreeQty2")
                    csch.SchemeMinQty3 = .Item("SchemeMinQty3")
                    csch.SchemeFreeQty3 = .Item("SchemeFreeQty3")
                    csch.DiscountType = .Item("DiscountType")
                    csch.Discount = .Item("Discount")
                    csch.schemeItemID = .Item("SchemeItemId")
                    csch.remarks = .Item("Remarks")
                    '@20071127:Add following line
                    csch.ApllyTo = BCommon.ChkNullVal(.Item("AppliesTo"), "")
                    '.Item("Status")
                End With
                Return csch
            Else : Return Nothing
            End If
        Catch ex As Exception
            DGeneral.ErrorHandler(ex.Message, "BAL", "BScheme", "GetScheme")
            Return Nothing
        End Try
    End Function

    Public Shared Function GetSchemeInfo(ByVal SchemeID As Integer) As DataTable
        Return DScheme.GetSchemeInfo(SchemeID)
    End Function

    Public Shared ReadOnly Property GetSchemeName(ByVal Scheme As CScheme) As String
        Get
            Dim strScheme As String = String.Empty
            If Scheme.SchemeID > 0 Then

                If Scheme.SchemeType = "D" Then
                    If Scheme.DiscountType = "A" Then
                        strScheme = Scheme.Discount.ToString() & " RS.Off"
                    ElseIf Scheme.DiscountType = "P" Then
                        strScheme = Scheme.Discount.ToString() & "% Off"
                    End If
                ElseIf Scheme.SchemeType = "F" Then
                    strScheme = DItem.GetItemName(Scheme.schemeItemID) & " Free"
                End If
            End If
            Return strScheme
        End Get
    End Property

    Public Shared Function GetSchemeQty(ByVal SchemeID As Integer, ByVal SchemeOnQty As Integer) As Integer
        Try
            Dim Scheme As CScheme = GetScheme(SchemeID)
            Return GetSchemeQty(Scheme, SchemeOnQty)
        Catch ex As Exception
            DGeneral.ErrorHandler(ex.Message, "BAL", "BScheme", "GetSchemeQty")
        End Try
    End Function

    Public Shared Function GetSchemeQty(ByVal Scheme As CScheme, ByVal SchemeOnQty As Integer) As Integer
        Try
            If SchemeOnQty = 0 Then Return 0
            Dim ApplicableQty As Integer = 0

            With Scheme
                While SchemeOnQty > 0
                    If .SchemeMinQty3 > 0 AndAlso SchemeOnQty >= .SchemeMinQty3 Then
                        ApplicableQty += (SchemeOnQty \ .SchemeMinQty3) * .SchemeFreeQty3
                        SchemeOnQty = SchemeOnQty Mod .SchemeMinQty3
                    ElseIf .SchemeMinQty2 > 0 AndAlso SchemeOnQty >= .SchemeMinQty2 Then
                        ApplicableQty += (SchemeOnQty \ .SchemeMinQty2) * .SchemeFreeQty2
                        SchemeOnQty = SchemeOnQty Mod .SchemeMinQty2
                    ElseIf .SchemeMinQty1 > 0 AndAlso SchemeOnQty >= .SchemeMinQty1 Then
                        ApplicableQty += (SchemeOnQty \ .SchemeMinQty1) * .SchemeFreeQty1
                        SchemeOnQty = SchemeOnQty Mod .SchemeMinQty1
                    Else
                        SchemeOnQty = 0
                    End If
                End While
                Return ApplicableQty
            End With
        Catch ex As Exception
            DGeneral.ErrorHandler(ex.Message, "BAL", "BScheme", "GetSchemeQty")
        End Try
    End Function

    Public Shared Function IsSchemeUsed(ByVal SchemeID As Integer) As Integer
        Return DScheme.IsSchemeUsed(SchemeID)
    End Function

    Public Shared Function DeleteScheme(ByVal SchemeID As Integer) As Integer
        Return DScheme.DeleteScheme(SchemeID)
    End Function

    Public Shared Function IsValidScheme(ByVal SchemeID As Integer) As Boolean
        Return DScheme.IsValidScheme(SchemeID)
    End Function

End Class
